Make the C extension Ractor-safe by removing msgpack_rmem_* slab allocator#392
Open
ianks wants to merge 1 commit into
Open
Make the C extension Ractor-safe by removing msgpack_rmem_* slab allocator#392ianks wants to merge 1 commit into
msgpack_rmem_* slab allocator#392ianks wants to merge 1 commit into
Conversation
msgpack_rmem_* slab allocator
28616a5 to
bb0785b
Compare
…or_safe) The page-recycling slab was a process-global msgpack_rmem_t mutated through an unsynchronized bitmask, so parallel Ractors (or threads) raced on it. Drop the slab and serve rmem pages straight from xmalloc/xfree (jemalloc and friends recycle these fine), then declare rb_ext_ractor_safe(true). The declaration is guarded by HAVE_RB_EXT_RACTOR_SAFE so the extension still builds on Ruby < 3.0. Add spec/cruby/ractor_spec.rb covering pack/unpack via Factory and Packer/Unpacker from non-main Ractors, including a concurrent multi-Ractor stress over the page-allocation path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes the extension safe to use from a non-main Ractor. The notable change here is the removal of the
msgpack_rmem_*routines, which serve as a mechanism for efficiently providing chunks of memory for decoding work.Why is it not ractor safe?
The old page-recycling slab is a process-global
msgpack_rmem_tmutated through an unsynchronized bitmask, so parallel Ractors (or threads) race on it.How did you address this?
Drop the global slab entirely and use plain
xmalloc/xfree. Modern arena-based mallocs (i.e. jemalloc) are good at recycling and avoiding thread contention, so maintaining a custom slab allocator is not worth it.Did you try alternatives?
Yes:
No:
malloc(3)Perf (local HTTP requests)
Single-threaded, jemalloc 5.3, decode-heavy workload
On a realistic, "real" HTTP request benchmark, bare-xmalloc is maybe a touch faster, but mostly noise in the diff:
RSS Impact
Memory usage seems fine as well, with the bare-xmalloc (this PR) having a higher peak as decay time increases (this is expected, and not a problem).
dirty_decay_ms:10000(default)dirty_decay_ms:1000closes #390